﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace C4F.DevKit.Calendar.Controls
{
    internal partial class SelectAttendees : Form
    {
        public string SelectedAttendees
        {
            get { return txtSelectedAttendees.Text; }
        }

        public SelectAttendees(List<Attendee> contacts, string currentAttendeeText)
        {
            InitializeComponent();

            bindingSource1.DataSource = contacts;
            grdAttendees.Columns[NameColumn.Index].DataPropertyName = "Name";
            grdAttendees.Columns[EmailColumn.Index].DataPropertyName = "EmailAddress";
            txtSelectedAttendees.Text = currentAttendeeText;

            if (!txtSelectedAttendees.Text.Trim().EndsWith(";"))
            {
                txtSelectedAttendees.Text += "; ";
            }
        }

        private void grdAttendees_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                string name = grdAttendees.Rows[e.RowIndex].Cells[NameColumn.Name].Value.ToString();
                string email = grdAttendees.Rows[e.RowIndex].Cells[EmailColumn.Name].Value.ToString();

                if (string.IsNullOrEmpty(email))
                {
                    txtSelectedAttendees.Text += string.Format("{0}; ", name);
                }
                else
                {
                    txtSelectedAttendees.Text += string.Format("{0} <{1}>; ", name, email);
                }
            }
        }
    }
}
